package com.example.securtydemo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.access.SecurityMetadataSource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;

import java.util.logging.Filter;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)//会拦截注解了@PreAuthrize注解的配置
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    /**
     * 通过复写configigure方法，进行创建用户
     */
    //protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    // 加密法一：
    //               auth.inMemoryAuthentication()
    //                     .passwordEncoder(new BCryptPasswordEncoder())
    //                     .withUser("admin")
    //                     .password(new BCryptPasswordEncoder().encode("000000"))
    //                     .roles();
    //                auth.inMemoryAuthentication()
    //                        .passwordEncoder(new BCryptPasswordEncoder())
    //                        .withUser("user")
    //                        .password(new BCryptPasswordEncoder().encode("000000"))
    //                        .roles();

    // 加密法二：
    //                auth.inMemoryAuthentication()
    //                        .withUser("admin")
    //                        .password(passwordEncoder().encode("000000"))
    //                        .roles();
    //                auth.inMemoryAuthentication()
    //                        .withUser("user")
    //                        .password(passwordEncoder().encode("000000"))
    //                        .roles();

    // 通过AuthenticationManagerBuilder的roles()方法，就可以指定角色。
    //        auth.inMemoryAuthentication()
    //                .withUser("admin")
    //                .password(passwordEncoder().encode("000000"))
    //                .roles("admin");
    //
    //        auth.inMemoryAuthentication()
    //                .withUser("user")
    //                .password(passwordEncoder().encode("000000"))
    //                .roles("normal");
    // }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin().loginPage("/login")
                .and()
                .authorizeRequests()
                .antMatchers("/login").permitAll() //允许所有人可以访问登陆页面
                .antMatchers("/", "/index").permitAll()
                .antMatchers("/test/**", "/test1/**", "/favicon.ico").permitAll()
                .antMatchers("/res/**/*.{js,html}").permitAll()

                .anyRequest().access("@authService.canAcess(request,authentication)")
                .and().sessionManagement().maximumSessions(1);
        //.anyRequest().authenticated();//所有的请求需要在登陆之后才能访问


    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
